﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sims3Package;
using System.IO;

namespace Sims3Package
{
    public class TGIBlockList: List<TGIBlock>
    {
        private TGIOrder order;
        /// <summary>
        /// Initialize a new instance of TGIBlockList with default TGIOrder(TGI) that is empty and has specified capacity
        /// </summary>
        /// <param name="capacity">Number of elements that a TGIBlock can hold</param>
        public TGIBlockList(int capacity) : base(capacity) { }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with default TGIOrder(TGI) that is empty and has default capacity
        /// </summary>
        public TGIBlockList() : base() { }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with given TGIOrder that is empty and has default capacity
        /// </summary>
        /// <param name="order"><see cref=">Sims3Package.TGIOrder"/></param>
        public TGIBlockList(TGIOrder order) : base() { this.order = order; }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with default TGIOrder(TGI) from stream
        /// </summary>
        /// <param name="s">Stream containing the TGI list. <see cref="System.IO.Stream"/></param>
        public TGIBlockList(Stream s) : this(new BinaryReader(s)) { }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with default TGIOrder(TGI) from binary reader
        /// </summary>
        /// <param name="r"><see cref="System.IO.BinaryReader"/></param>
        public TGIBlockList(BinaryReader r) : this(r, TGIOrder.TGI) { }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with given TGIOrder from stream
        /// </summary>
        /// <param name="s">Stream containing the TGI list. <see cref="System.IO.Stream"/></param>
        /// <param name="order"><see cref=">Sims3Package.TGIOrder"/></param>
        public TGIBlockList(Stream s, TGIOrder order) : this(new BinaryReader(s), order) { }
        /// <summary>
        /// Initialize a new instance of TGIBlockList with given TGIOrder from binary reader
        /// </summary>
        /// <param name="r"><see cref="System.IO.BinaryReader"/></param>
        /// <param name="order"><see cref=">Sims3Package.TGIOrder"/></param>
        public TGIBlockList(BinaryReader r, TGIOrder order)
        {
            int tgi_offest = r.ReadInt32();
            long TGI_POSITION = tgi_offest + r.BaseStream.Position;
            int TGI_SIZE = r.ReadInt32();
            this.order = order;
            long position = r.BaseStream.Position;
            r.BaseStream.Position = TGI_POSITION;
            for (int i = 0; i < (TGI_SIZE) / 16; i++)
            {
                base.Add(new TGIBlock(r, order));
            }
            r.BaseStream.Position = position;
        }
        /// <summary>
        /// Unparse the TGIlist into BinaryWriter
        /// </summary>
        /// <param name="r"><see cref="System.IO.BinaryWriter"/></param>
        public void UnParse(BinaryWriter r)
        {
            
        }

        public string Value
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("TGI Reference List: Count 0x{0:X2}", (byte)this.Count).AppendLine();
                for (int i = 0; i < this.Count; i++) sb.AppendFormat("0x{0:X2}: {1}", i, this[i]).AppendLine();
                return sb.ToString();
            }
        }
        
        /// <summary>
        /// Unparse the TGIlist into stream
        /// </summary>
        /// <param name="s">Stream to be written TGI list. <see cref="System.IO.Stream"/></param>
        public void UnParse(Stream s)
        {
            UnParse(new BinaryWriter(s));
        }

    }
}
